package com.classbook.violet.baigiang6.fragment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.classbook.violet.baigiang6.activity.MainActivity;
import com.classbook.violet.baigiang6.adapter.ListLessonAdapter;
import com.classbook.violet.baigiang6.adapter.ListMenuAdapter;
import com.classbook.violet.baigiang6.database.DatabaseHelper;
import com.classbook.violet.baigiang6.subject.LessonNode;
import com.classbook.violet.baigiang6.subject.SubjectParser;
import com.classbook.violet.baigiang6.subject.UnitNode;
import com.classbook.violet.baigiang7.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MenuFragment extends Fragment implements OnRefreshListener{
	
	private final String PATH = Environment.getExternalStorageDirectory().getPath() + "/EDC/Violet/";
	
	ListView lv_manage;
	ListView lv_lesson;
//	List<String> listManage;
	List<UnitNode> listUnit;
//	ListUnitAdapter listUnitAdapter;
	List<LessonNode> listLesson;
//	ListManageAdapter listManageAdapter;
	ListLessonAdapter listLessonAdapter;
	ListMenuAdapter listMenuAdapter;
	SubjectParser mParser;
	
	DatabaseHelper db;
	
	public MenuFragment(){}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.menu, null);
		db = new DatabaseHelper(getActivity());
		Log.e("MenuFragment", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		listLesson = new ArrayList<LessonNode>();
		listUnit = new ArrayList<UnitNode>();
		listUnit.add(new UnitNode("Bài giảng đã xem"));
		listUnit.add(new UnitNode("Bài giảng tải về"));
		listUnit.add(new UnitNode("Bài giảng ưa thích"));
		
		mParser = ((MainActivity)getActivity()).getParser();
		Log.e("MenuFragment", "CREAT VIEW");
		lv_lesson = (ListView) view.findViewById(R.id.lv_lesson);
		lv_manage = (ListView) view.findViewById(R.id.lv_unit);
		listUnit.get(0).ischeck = true;
		listMenuAdapter = new ListMenuAdapter(getActivity(), listUnit);
		lv_manage.setAdapter(listMenuAdapter);
		listLesson = new ArrayList<LessonNode>();
		listLesson = db.getAllRecent();
		checkThumb(listLesson);
		checkLike(listLesson);
		listLessonAdapter = new ListLessonAdapter(getActivity(), listLesson, "menu-recent");
		lv_lesson.setAdapter(listLessonAdapter);
		listLessonAdapter.notifyDataSetChanged();
		
		lv_manage.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				listMenuAdapter.disableCheck();
				listMenuAdapter.getItem(arg2).ischeck = true;
				listMenuAdapter.notifyDataSetChanged();
				Log.i("LIST MENU", listUnit.get(arg2).label);
				if (listUnit.get(arg2).label.contains("Bài giảng tải về")){
					listLesson = new ArrayList<LessonNode>();
					getAllDownloaded();
					checkLike(listLesson);
					listLessonAdapter = new ListLessonAdapter(getActivity(), listLesson, "menu-downloaded");
				}else if (listUnit.get(arg2).label.contains("Bài giảng đã xem")){
					listLesson = new ArrayList<LessonNode>();
					getAllDownloaded();
					List<LessonNode> list_temp = new ArrayList<LessonNode>();
					for (int i = 0; i < listLesson.size(); i++){
						if (db.checkLessonRecent(listLesson.get(i))){
							list_temp.add(listLesson.get(i));
						}
					}
					listLesson = list_temp;
					listLessonAdapter = new ListLessonAdapter(getActivity(), listLesson, "menu-like");
				}
				else if (listUnit.get(arg2).label.contains("Bài giảng ưa thích")){
					listLesson = new ArrayList<LessonNode>();
					getAllDownloaded();
					for (int i = 0; i < listLesson.size(); i++){
						if (!db.checkLessonCategory("like", listLesson.get(i))){
							listLesson.remove(i);
							i--;
						}else{
							listLesson.get(i).isLike = true;
						}
					}
					
					listLessonAdapter = new ListLessonAdapter(getActivity(), listLesson, "menu-like");
				}
				lv_lesson.setAdapter(listLessonAdapter);
				listLessonAdapter.notifyDataSetChanged();
			}
		});
		
		return view;
	}
	
	public void getAllDownloaded (){
		getLessonFromSubject("Đại số 7", "THCS/Dai so/Dai so 7/");
		getLessonFromSubject("Hình học 7", "THCS/Hinh hoc/Hinh hoc 7/");
		getLessonFromSubject("Ngữ văn 7", "THCS/Ngu Van/Ngu Van 7/");
		getLessonFromSubject("Tiếng Anh 7", "THCS/Tieng Anh/Tieng Anh 7/");
		getLessonFromSubject("Vật lí 7", "THCS/Vat li/Vat li 7/");
		getLessonFromSubject("Hóa học 7", "THCS/Hoa hoc/Hoa hoc 7/");
		getLessonFromSubject("Sinh học 7", "THCS/Sinh hoc/Sinh hoc 7/");
		getLessonFromSubject("Lịch sử 7", "THCS/Lich su/Lich su 7/");
		getLessonFromSubject("Địa lí 7", "THCS/Dia li/Dia li 7/");
	}
	
	public void getLessonFromSubject (String subject, String file_link){
		Log.v("PARSER MENU", subject);
		List<LessonNode> listLesonS = mParser.parseLesson(subject);
		Log.i("MENUFRAGMENT", "" + listLesonS.size());
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 8;
		for (int i = 0; i < listLesonS.size(); i++){
			listLesonS.get(i).path = file_link + listLesonS.get(i).url;
			File video = new File(PATH + listLesonS.get(i).path + ".wmv");
			File ppt = new File(PATH + listLesonS.get(i).path + ".ppt");
			if (video.exists() || ppt.exists()){
				listLesonS.get(i).cover = BitmapFactory.decodeFile(PATH + listLesonS.get(i).path + ".jpg");
				listLesson.add(listLesonS.get(i));
			}
		}
	}
	
	public void checkThumb (List<LessonNode> lessonList){
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 8;
		for (int i = 0; i < lessonList.size(); i++){
			File thumb = new File(PATH + lessonList.get(i).path + ".jpg");
			if (thumb.exists()){
				Bitmap bm =  BitmapFactory.decodeFile(thumb.getAbsolutePath()) ;
				lessonList.get(i).cover = Bitmap.createScaledBitmap(bm, 160, 120, true);
				bm.recycle();
			}
		}
	}
	
	public void checkLike (List<LessonNode> lessonList){
		for (int i = 0; i < lessonList.size(); i++){
			lessonList.get(i).isLike = db.checkLessonCategory("like", lessonList.get(i));
		}
	}


	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		
	}
	
}
